package com.vitabuybuy.tv.ui.mine.talent.task

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentTaskManageBinding
import com.vitabuybuy.tv.dto.TaskPoolDto
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.ui.mine.talent.task.viewmodel.TaskViewModel
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.fragment_task_manage.*
import kotlinx.android.synthetic.main.fragment_task_manage.refresh

class TaskManageFragment : BaseFragment<TaskViewModel, FragmentTaskManageBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_task_manage

    private var pageNumber = 1
    private var type = "invitation"//	类型（邀请任务传invitation，报名传sign）
    private var params = HashMap<String, Any>()
    private var pageMap = HashMap<String, Any>().apply {
        this["size"] = 10
        this["desc"] = "id"
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel
    }

    override fun lazyLoadData() {
        searchCondition(pageNumber)
    }

    private fun searchCondition(pageNumber: Int) {
        pageMap["page"] = pageNumber
        params["pageable"] = pageMap
        params["type"] = type
        viewModel.getTaskManagerList(pageNumber, params)
    }


    override fun eventHandling() {
        setOnclickNoRepeat(tvInviteTask, tvApplyTask){v->
            when (v) {
                tvInviteTask -> {
                    tvInviteTask.setTextColor(Color.parseColor("#E23225"))
                    tvApplyTask.setTextColor(Color.parseColor("#151F33"))
                    type = "invitation"
                    pageNumber = 1
                    searchCondition(pageNumber)
                }
                tvApplyTask -> {
                    tvApplyTask.setTextColor(Color.parseColor("#E23225"))
                    tvInviteTask.setTextColor(Color.parseColor("#151F33"))
                    type = "sign"
                    pageNumber = 1
                    searchCondition(pageNumber)
                }
            }
        }
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                searchCondition(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                searchCondition(pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                searchCondition(pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                val item = msg.obj as TaskPoolDto
                when (type) {
                    "sign" -> {
                        ApplyTaskDetailActivity.openActivity(mContext, item.id)
                    }
                    else -> {
                        InviteTaskDetailActivity.openActivity(mContext, item.id)
                    }
                }
            }
        }
    }

    override fun onEvent(event: MessageEvent) {
        when (event.code) {
            EventCode.refush_task_manage_list -> {
                pageNumber = 1
                searchCondition(pageNumber)
            }
        }
    }
}
